﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProbeGUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private string searchResult = "";

        private delegate void DelegateVoidVoid();

        public Window1()
        {
            InitializeComponent();
        }

        private void LayoutRoot_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
                this.DragMove();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        void ExecuteAddItem(object sender, ExecutedRoutedEventArgs e)
        {
        }

        void ExecuteEditItem(object sender, ExecutedRoutedEventArgs e)
        {
        }

        void ExecuteDeleteItem(object sender, ExecutedRoutedEventArgs e)
        {
        }

        void ExecuteHelp(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://bingprobe.codeplex.com/");
        }

        private void OnSearchComplete()
        {
            if (string.IsNullOrEmpty(searchResult))
            {
                this.tbMultiLine.Text = "";
                MessageBox.Show(
                    "No results were returned. Please verify that you have entered a valid IP address or DNS name.",
                    "Search Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            else
            {
                this.tbMultiLine.Text = searchResult;
            }
        }

        private void OnOperationComplete(IAsyncResult result)
        {
            ((Action)result.AsyncState).EndInvoke(result);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            string searchText = this.SearchTextBox.Text;
            this.tbMultiLine.Text = "Searching ...";

            Action a = new Action(delegate ()
            {
                BingProbe bp = new BingProbe();
                searchResult = bp.ListSites(searchText);

                Dispatcher.Invoke(new DelegateVoidVoid(OnSearchComplete), null);
            });

            a.BeginInvoke(OnOperationComplete, a);
        }

        private void PasswordsListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        }

        private void SearchTextBox_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            if (false == string.IsNullOrEmpty(SearchTextBox.Text))
                SearchTextBox.Select(0, SearchTextBox.Text.Length);
        }

        private void SearchTextBox_GotMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (false == string.IsNullOrEmpty(SearchTextBox.Text))
                SearchTextBox.Select(0, SearchTextBox.Text.Length);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SearchTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return)
                return;

            e.Handled = true;
            this.SearchButton_Click(sender, null);
        }

        private void tbMultiLine_GotFocus(object sender, RoutedEventArgs e)
        {
            if (false == string.IsNullOrEmpty(tbMultiLine.Text))
                tbMultiLine.Select(0, tbMultiLine.Text.Length);
        }
    }

    public class AppCommands
    {
        static public RoutedUICommand AddItemCommand = new RoutedUICommand("Add Item", "AddItem", typeof(Window1));
        static public RoutedUICommand EditItemCommand = new RoutedUICommand("Edit Item", "EditItem", typeof(Window1));
        static public RoutedUICommand DeleteItemCommand = new RoutedUICommand("Delete Item", "DeleteItem", typeof(Window1));
        static public RoutedUICommand HelpCommand = new RoutedUICommand("Help", "Help", typeof(Window1));
    }
}
